package com.genghis.ptas.web;

import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.scorm.entity.ScormInfo;
import com.genghis.ptas.scorm.service.UpScormService;
import com.genghis.ptas.security.entity.Role;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.service.RoleService;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/")
public class HomeController {
    @Autowired
    private UserService userService;
    @Autowired
    private UpScormService upScormService;


    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    public String index(HttpServletRequest request) {
        HttpSession session = request.getSession();
        String loginName = LoginUserUtil.findLoginUserName();
        if(loginName==null){
            List<ScormInfo> scormInfoList = upScormService.findScormInfoPageList();
            request.setAttribute("scormInfoList",scormInfoList);
            return "tourist/touristsmain";
        }
        User user = userService.findUserByLoginName(loginName);
        session.setAttribute("userId", user.getId());
        session.setAttribute("user", user);
        if (user.getUserRole()==0) {
            List<ScormInfo> scormScoreList = upScormService.listAllScormByAvgScore();
            List<ScormInfo> scormRegistNumList = upScormService.listAllScormByRegistNum();
            List<ScormInfo> scormLoadTimeList = upScormService.listAllScormByUploadTime();
            request.setAttribute("scormLoadTimeList",scormLoadTimeList);
            request.setAttribute("scormRegistNumList",scormRegistNumList);
            request.setAttribute("scormScoreList",scormScoreList);
            return "/index2";
        } else {
            return "/index";
        }
    }

    @RequestMapping("lock_screen.*")
    public String displayLockScreenPage() {
        return "lockScreen";
    }
}